<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">供应商：</label>
                  <div class="layui-input-inline">
                    <input name="corporateName" class="layui-input" type="text" placeholder="请输入供应商名称" />
                  </div>
                </div>
                <!-- <div class="layui-inline">
                  <label class="layui-form-label w-auto">联系人姓名：</label>
                  <div class="layui-input-inline">
                    <input name="businessContactName" class="layui-input" type="text" placeholder="请输入供应商名称" />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">联系人电话：</label>
                  <div class="layui-input-inline">
                    <input name="phone" class="layui-input" type="text" placeholder="请输入供应商名称" />
                  </div>
                </div> -->
                <div class="layui-inline">
                  <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                  <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                </div>
              </div>
              <div class="query-rt"></div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button id="purchaseDeliveryRecordBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
              <button id="confirmSelect" class="layui-btn icon-btn color-reseda" style="display: none">确认选择</button>
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table class="layui-table" id="purchaseDeliveryRecordTable" lay-filter="purchaseDeliveryRecordTable"></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseDeliveryRecordTableBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">详情</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="purchaseDeliveryRecordForm">
  <form lay-filter="orderReceiveForm" class="layui-form open-form">
    <div class="open-model">
      <div class="open-auto" style="padding-top: 10px;">
        <input name="id" type="hidden" />
        <div class="layui-col-xs3">
          <label class="layui-form-label" style="width: 100px;"><span style="color:red;">*</span>采购订单编号</label>
          <div class="layui-input-block" style="margin-left: 130px;display: flex;align-items: center;">
            <input name="purchaseOrderId" disabled class="layui-input" type="hidden" autocomplete="off" readonly />
            <input name="sn" disabled class="layui-input" type="text" autocomplete="off" readonly />
            <!-- <select id="purchaseOrderId" name="purchaseOrderId" lay-filter="purchaseOrderId" lay-search lay-verify="required" required></select> -->
            <button class="layui-btn layui-btn-xs" type="button" id="checkPurchaseOrder">选择采购订单</button>
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label" style="width: 100px;">供应商</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="supplierName" class="layui-input" type="text" autocomplete="off" readonly />
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="businessContactName" class="layui-input" type="text" autocomplete="off" readonly />
          </div>
        </div>
        <div class="layui-col-xs3">
          <label class="layui-form-label" style="width: 100px;">采购部门</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="departmentName" class="layui-input" type="text" autocomplete="off" readonly />
          </div>
        </div>
        <div class="layui-col-xs3" style="margin-top: 10px;">
          <label class="layui-form-label" style="width: 100px;">运输方式</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <select id="transportMode" name="transportMode" lay-filter="transportMode" lay-search lay-verify="required" required></select>
          </div>
        </div>
        <div class="layui-col-xs3" style="margin-top: 10px;">
          <label class="layui-form-label" style="width: 100px;">物流公司</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="logisticsCompany" class="layui-input" type="text" autocomplete="off" placeholder="请输入物流公司" />
          </div>
        </div>
        <div class="layui-col-xs3" style="margin-top: 10px;">
          <label class="layui-form-label" style="width: 100px;">运单编号</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="waybillNo" class="layui-input" lay-verify="required" required type="text" autocomplete="off" placeholder="请输入运单编号" />
          </div>
        </div>
        <div class="layui-col-xs3" style="margin-top: 10px; display: none;" id="plateNoDiv">
          <label class="layui-form-label" style="width: 100px;">车牌号</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="plateNo" class="layui-input" type="text" autocomplete="off" placeholder="请输入车牌号" />
          </div>
        </div>
        <div class="layui-col-xs3" style="margin-top: 10px; display: none;" id="courierNoDiv">
          <label class="layui-form-label" style="width: 100px;">快递单号</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="courierNo" class="layui-input" type="text" autocomplete="off" placeholder="请输入快递单号" />
          </div>
        </div>
        <div class="layui-col-xs3" style="margin-top: 10px;">
          <label class="layui-form-label" style="width: 100px;">电话</label>
          <div class="layui-input-block" style="margin-left: 130px;">
            <input name="phone" class="layui-input" type="text" autocomplete="off" placeholder="请输入电话" />
          </div>
        </div>
        <div>
          <table class="layui-table"></table>
          <div class="layui-btn" id="addProduct" style="margin-top: 10px;">添加发货物料</div>
          <div id="productList">
            <table class="layui-table" id="purchaseDeliveryRecordProductTable" lay-filter="purchaseDeliveryRecordProductTable"></table>
          </div>
        </div>
      </div>
      <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="purchaseDeliveryRecordFormSubmit" lay-submit>提交</button>
      </div>
    </div>
  </form>
</script>

<!--发货物料-->
<script type="text/html" id="purchaseDeliveryRecordProductList">
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <!-- 头部操作栏 -->
        <div class="layui-form lay-header">
          <div class="toolbar">
            <div class="layui-form-item lay-query-content">
              <div class="lay-query-box">
                <div class="query-lt">
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">物料编号：</label>
                    <div class="layui-input-inline mr0">
                      <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号" />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">物料名称：</label>
                    <div class="layui-input-inline mr0">
                      <input name="productName" class="layui-input" type="text" placeholder="请输入物料名称" />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <button class="layui-btn icon-btn color-green" lay-filter="deliveryRecordProductFormSubSearchLog" lay-submit>查询</button>
                    <button class="layui-btn icon-btn color-white" lay-filter="deliveryRecordProductResets" lay-submit>重置</button>
                    <button id="confirmChoose" class="layui-btn icon-btn color-reseda">确认选择</button>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="lay-btn-rows">
            <div class="lay-btn-box"></div>
          </div>
        </div>
        <!-- 表格 -->
        <table class="layui-table" id="purchaseDeliveryRecordProductListTable" lay-filter="purchaseDeliveryRecordProductListTable"></table>
      </div>
    </div>
  </div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseDeliveryRecordProductTableBar">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 详情弹窗 -->
<script type="text/html" id="purchaseDeliveryRecordView">
  <form lay-filter="purchaseDeliveryRecordView" class="layui-form model-form" style="padding: 10px 20px;">
    <input name="id" type="hidden" />
    <div class="layui-col-xs3">
      <label class="layui-form-label" style="width: 100px;">采购订单编号</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="sn" class="layui-input" type="text" autocomplete="off" readonly />
      </div>
    </div>
    <div class="layui-col-xs3">
      <label class="layui-form-label" style="width: 100px;">供应商</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="corporateName" class="layui-input" type="text" autocomplete="off" readonly />
      </div>
    </div>
    <div class="layui-col-xs3">
      <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="businessContactName" class="layui-input" type="text" autocomplete="off" readonly />
      </div>
    </div>
    <div class="layui-col-xs3">
      <label class="layui-form-label" style="width: 100px;">采购部门</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="departmentName" class="layui-input" type="text" autocomplete="off" readonly />
      </div>
    </div>
    <div class="layui-col-xs3" style="margin-top: 10px;">
      <label class="layui-form-label" style="width: 100px;">运输方式</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="transportModeDesc" class="layui-input" type="text" autocomplete="off" readonly />
      </div>
    </div>
    <div class="layui-col-xs3" style="margin-top: 10px;">
      <label class="layui-form-label" style="width: 100px;">物流公司</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="logisticsCompany" class="layui-input" type="text" autocomplete="off" readonly />
      </div>
    </div>
    <div class="layui-col-xs3" style="margin-top: 10px;">
      <label class="layui-form-label" style="width: 100px;">运单编号</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="waybillNo" class="layui-input" type="text" autocomplete="off" />
      </div>
    </div>
    <div class="layui-col-xs3" style="margin-top: 10px; display: none;" id="courierNoDetailDiv">
      <label class="layui-form-label" style="width: 100px;">快递单号</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="courierNo" class="layui-input" type="text" autocomplete="off" />
      </div>
    </div>
    <div class="layui-col-xs3" style="margin-top: 10px; display: none;" id="plateNoDetailDiv">
      <label class="layui-form-label" style="width: 100px;">车牌号</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="plateNo" class="layui-input" type="text" autocomplete="off" />
      </div>
    </div>
    <div class="layui-col-xs3" style="margin-top: 10px;">
      <label class="layui-form-label" style="width: 100px;">电话</label>
      <div class="layui-input-block" style="margin-left: 130px;">
        <input name="phone" class="layui-input" type="text" autocomplete="off" />
      </div>
    </div>
    <table class="layui-table" id="purchaseDeliveryRecordProductTable" lay-filter="purchaseDeliveryRecordProductTable"></table>
  </form>
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "admin", "laytpl", "laydate", "util"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    let nowData = {};
    if (typeof parentData !== "undefined") {
      nowData = { ...parentData };
    }
    if (nowData.eventName == "radio") {
      $("#confirmSelect").show();
      $("#purchaseDeliveryRecordBtnAdd").hide();
    }

    form.render("select");
    admin.selectSearch();

    $("#confirmSelect").click(function () {
      let list = table.checkStatus("purchaseDeliveryRecordTable").data;
      childData.list = list;
      layer.close(nowData.zindex);
    });

    // 渲染表格
    var insTb = table.render({
      elem: "#purchaseDeliveryRecordTable",
      url: config.base + "enterprise/purchase/admin/purchaseDeliveryRecord/list",
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          {
            type: "radio",
            hide: nowData.eventName != "radio"
          },
          { type: "numbers", title: "序号", width: 55 },
          {
            field: "logisticsNo",
            title: "物流记录编号",
            align: "center",
            width: 160
          },
          { field: "sn", title: "采购单编号", align: "center", width: 160 },
          {
            field: "corporateName",
            title: "供应商",
            align: "center"
          },
          { field: "skuNum", title: "发货物料数", align: "center" },
          { field: "totalAmount", title: "金额", align: "center" },
          { field: "transportModeDesc", title: "运输方式", align: "center" },
          { field: "logisticsCompany", title: "物流公司", align: "center" },
          {
            field: "waybillNo",
            title: "运单号",
            align: "center",
            width: 160
          },
          { field: "statusName", title: "状态", align: "center" },
          {
            field: "createTime",
            title: "创建时间",
            align: "center",
            width: 160
          },
          {
            align: "center",
            toolbar: "#purchaseDeliveryRecordTableBar",
            title: "操作",
            width: 100,
            fixed: "right"
          }
        ]
      ],
      done: function (res, curr, count) {
        //移除按钮
        var menuButton = config.getMenuButton(location.hash);
        if (menuButton.indexOf("搜索") == -1) {
          $(".layui-form.toolbar").remove();
        }
        if (menuButton.indexOf("修改") == -1) {
          $("[lay-event='edit']").remove();
        }
        if (menuButton.indexOf("删除") == -1) {
          $("[lay-event='del']").remove();
        }
      }
    });

    // 工具条点击事件
    table.on("tool(purchaseDeliveryRecordTable)", function (obj) {
      var data = obj.data;
      if (obj.event === "edit") {
        //修改
        showEditModel(data);
      } else if (obj.event === "del") {
        // 删除
        doDelete(obj);
      } else if (obj.event === "view") {
        // 详情
        showViewModel(data);
      }
    });

    //监听排序
    table.on("sort(purchaseDeliveryRecordTable)", function (obj) {
      table.reload("purchaseDeliveryRecordTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type
        }
      });
    });

    // 添加按钮点击事件
    $("#purchaseDeliveryRecordBtnAdd").click(function () {
      showEditModel();
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", function (data) {
      insTb.reload({ where: admin.resetSearch(data.field), page: { curr: 1 } }, "data");
    });

    // 显示编辑弹窗
    function showEditModel(data) {
      admin.open({
        type: 1,
        area: ["80%", "85%"],
        offset: ["10%", "10%"],
        title: data ? "修改供应商发货记录" : "添加供应商发货记录",
        content: $("#purchaseDeliveryRecordForm").html(),
        success: function () {
          $("#checkPurchaseOrder").click(function () {
            admin.formOpen({
              area: "90%",
              offset: "50px",
              title: "选择采购订单",
              path: "components/purchase/purchaseOrder.html",
              success: function () {
                parentData.eventName = "radio";
                parentData.where = {
                  // filterDeliveryComplete: 1
                  filterInfo: "2"
                };
              },
              end: function () {
                let list = childData.list;
                if (list.length > 0) {
                  deliveryRecordProductList = [];
                  table.reload("purchaseDeliveryRecordProductTable", {
                    data: []
                  });
                  let obj = list[0];
                  console.log(obj);
                  $("input[name='purchaseOrderId']").val(obj.id);
                  $("input[name='sn']").val(obj.sn);
                  admin.req(
                    "enterprise/purchase/admin/purchaseOrder/list",
                    { limit: 99999, sn: obj.sn, status: "success" },
                    function (data) {
                      if (data.code == 200) {
                        $("input[name='supplierName']").val(data.data[0].supplierName);
                        $("input[name='businessContactName']").val(data.data[0].businessContactName);
                        $("input[name='departmentName']").val(data.data[0].departmentName);
                      } else {
                        layer.msg(data.msg, { icon: 2, time: 1000 });
                      }
                    },
                    "GET"
                  );
                }
              }
            });
          });
          //采购订单列表
          admin.initDataSelect(
            "enterprise/purchase/admin/purchaseOrder/list",
            { limit: 99999, filterDeliveryComplete: 1, status: "success" },
            "purchaseOrderId",
            data ? data.purchaseOrderId : null,
            "采购订单",
            "sn",
            null
          );
          //采购订单选择事件
          form.on("select(purchaseOrderId)", function (selectData) {
            let elem = selectData.elem;
            let purchaseOrderId = elem[elem.selectedIndex].value;
            let purchaseOrderSn = elem[elem.selectedIndex].text;
            admin.req(
              "enterprise/purchase/admin/purchaseOrder/list",
              {
                limit: 99999,
                filterDeliveryComplete: 1,
                sn: purchaseOrderSn,
                status: "success"
              },
              function (data) {
                if (data.code == 200) {
                  $("input[name='supplierName']").val(data.data[0].supplierName);
                  $("input[name='businessContactName']").val(data.data[0].businessContactName);
                  $("input[name='departmentName']").val(data.data[0].departmentName);
                } else {
                  layer.msg(data.msg, { icon: 2, time: 1000 });
                }
              },
              "GET"
            );
          });
          //运输方式
          admin.getDictList("transportMode", "transport_mode", data ? data.transportMode : null, "运输方式");
          //运输方式选择事件
          form.on("select(transportMode)", function (selectData) {
            let transportMode = selectData.value;
            if (transportMode == 0) {
              $("#plateNoDiv").show();
              $("#courierNoDiv").hide();
            } else {
              $("#plateNoDiv").hide();
              $("#courierNoDiv").show();
            }
          });
          //发货数量校验
          form.verify({
            quantity: [/^[0-9]*(\.[0-9]{1,2})?$/, "格式错误"],
            compare: function (data, item) {
              var quantity = Number($(item).parent().parent().parent().prev().find("div").text());
              if (quantity < Number(data)) {
                return "发货数量不能大于剩余发货数！";
              }
            }
          });
          form.render();

          var codeArr = [];
          var deliveryRecordProductList = [];

          // 添加发货物料
          $("#addProduct").click(function () {
            var purchaseOrderId = $("form input[name='purchaseOrderId']").val();
            if (purchaseOrderId) {
              var purchaseDeliveryRecordProductList = admin.open({
                type: 1,
                area: ["80%", "85%"],
                offset: ["10%", "17%"],
                title: "物料列表",
                content: $("#purchaseDeliveryRecordProductList").html(),
                success: function () {
                  var codes = "";
                  for (var i = 0; i < codeArr.length; i++) {
                    if (i == 0) {
                      codes = '"' + codeArr[i] + '"';
                    } else {
                      codes += "," + '"' + codeArr[i] + '"';
                    }
                  }

                  // 渲染表格
                  var insTb = table.render({
                    elem: "#purchaseDeliveryRecordProductListTable",
                    url: config.base + "enterprise/purchase/admin/purchaseOrderDetail/list",
                    where: {
                      purchaseOrderId: purchaseOrderId,
                      filterDeliveryComplete: 1,
                      codes:
                        // codes
                        table.cache["purchaseDeliveryRecordProductTable"] ? table.cache["purchaseDeliveryRecordProductTable"].map((v) => v.skuCode).join(",") : ""
                    },
                    page: false,
                    cellMinWidth: 100,
                    cols: [
                      [
                        { type: "checkbox" },
                        {
                          field: "id",
                          align: "center",
                          title: "ID",
                          hide: "true"
                        },
                        {
                          field: "skuCode",
                          align: "center",
                          title: "物料编号",
                          width: 160
                        },
                        {
                          field: "productName",
                          align: "center",
                          title: "物料名称",
                          width: 160
                        },
                        {
                          field: "specifications",
                          align: "center",
                          title: "规格型号",
                          width: 160
                        },
                        { field: "quantity", align: "center", title: "数量" },
                        {
                          field: "unitName",
                          align: "center",
                          title: "计量单位",
                          width: 100
                        },
                        { field: "rate", align: "center", title: "税率(%)" },
                        {
                          field: "taxPrice",
                          align: "center",
                          title: "含税单价"
                        },
                        { field: "tax", align: "center", title: "税额" },
                        {
                          field: "totalAmount",
                          align: "center",
                          title: "合计金额"
                        }
                      ]
                    ],
                    done: function () {
                      let tableData = layui.table.cache["purchaseDeliveryRecordProductListTable"];

                      tableData.forEach((info, index) => {
                        let rate = info.rate / 100;
                        let taxPrice = info.taxPrice;
                        let quantity = info.quantity;
                        let subtotal = (rate * taxPrice * quantity).toFixed(2);
                        //赋值
                        admin.setTableColHtml("purchaseDeliveryRecordProductListTable", index, "tax", subtotal);
                      });
                    }
                  });

                  // 搜索
                  form.on("submit(deliveryRecordProductFormSubSearchLog)", function (data) {
                    insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
                  });

                  // 重置搜索
                  form.on("submit(deliveryRecordProductResets)", function (data) {
                    $(".lay-header").find("select").val("");
                    $(".lay-header").find("input").val("");
                    let field = data.field;
                    for (let key in field) {
                      field[key] = "";
                    }
                    insTb.reload({ where: field, page: { curr: 1 } }, "data");
                  });

                  // 确认选择
                  $("#confirmChoose").click(function () {
                    var checkStatus = table.checkStatus("purchaseDeliveryRecordProductListTable"); //test即为基础参数id对应的值
                    var dataS = checkStatus.data;
                    if (dataS.length == 0) {
                      layer.msg("请选择物料", { icon: 2, time: 1000 });
                    } else {
                      for (var i = 0; i < dataS.length; i++) {
                        deliveryRecordProductList.push(dataS[i]);
                        codeArr.push(dataS[i].skuCode);
                      }
                      layer.close(purchaseDeliveryRecordProductList);
                      console.log(123, deliveryRecordProductList);

                      // 获取数据集合并传入表格中
                      table.reload("purchaseDeliveryRecordProductTable", {
                        data: deliveryRecordProductList,
                        limit: deliveryRecordProductList.length
                      });
                    }
                  });
                }
              });
            } else {
              layer.msg("请选择采购订单!", { icon: 2, time: 1000 });
            }
          });

          // 渲染表格
          var insTb = table.render({
            elem: "#purchaseDeliveryRecordProductTable",
            data: [deliveryRecordProductList],
            page: false,
            cellMinWidth: 100,
            cols: [
              [
                { type: "numbers", title: "序号", width: 55 },
                { field: "id", title: "采购订单明细ID", hide: "true" },
                {
                  field: "productPic",
                  title: "物料图片",
                  align: "center",
                  templet: function (d) {
                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>';
                  }
                },
                { field: "skuCode", title: "物料编号", align: "center" },
                { field: "specifications", title: "规格号", align: "center" },
                {
                  field: "productTypeName",
                  title: "物料类型",
                  align: "center"
                },
                { field: "unitName", title: "计价单位", align: "center" },
                { field: "quantity", title: "采购数量", align: "center" },
                {
                  field: "hasDeliveryQuantity",
                  title: "已发货数",
                  align: "center"
                },
                {
                  field: "surplusDeliveryQuantity",
                  title: "剩余发货数",
                  align: "center",
                  templet: function (d) {
                    return (d.quantity - d.hasDeliveryQuantity).toFixed(2);
                  }
                },
                {
                  field: "deliveryQuantity",
                  title: "发货数量",
                  align: "center",
                  edit: "text"
                },
                {
                  align: "center",
                  toolbar: "#purchaseDeliveryRecordProductTableBar",
                  title: "操作",
                  width: 100
                }
              ]
            ]
          });

          // 工具条点击事件
          table.on("tool(purchaseDeliveryRecordProductTable)", function (obj) {
            var data = obj.data;
            if (obj.event === "del") {
              // 删除
              doDelete(obj);
            }
          });

          // 删除发货物料
          function doDelete(obj) {
            var index = $(obj.tr).data("index");
            layer.confirm(
              "确定要删除吗？",
              {
                offset: "65px",
                skin: "layui-layer-admin"
              },
              function (i) {
                layer.close(i);
                var skuCode = obj.data.skuCode;
                if (codeArr.indexOf(skuCode) != -1) {
                  deliveryRecordProductList.splice(index, 1);
                  codeArr.splice(codeArr.indexOf(skuCode), 1);
                }
                table.reload("purchaseDeliveryRecordProductTable", {
                  data: deliveryRecordProductList,
                  limit: deliveryRecordProductList.length
                });
              }
            );
          }

          form.val("purchaseDeliveryRecordForm", data);
          // 表单提交事件
          form.on("submit(purchaseDeliveryRecordFormSubmit)", function (d) {
            var purchaseDeliveryRecordProducts = [];
            $("#productList")
              .find("tbody tr")
              .each(function (index, item) {
                var purchaseDeliveryRecordProduct = {};
                $(item)
                  .find("td")
                  .each(function (tdIndex, tdItem) {
                    if ($(tdItem).data("field") == "id") {
                      purchaseDeliveryRecordProduct.purchaseOrderDetailId = $(tdItem).find("div").text();
                    }
                    if ($(tdItem).data("field") == "deliveryQuantity") {
                      purchaseDeliveryRecordProduct.quantity = $(tdItem).find("div").text();
                    }
                  });
                purchaseDeliveryRecordProducts.push(purchaseDeliveryRecordProduct);
              });
            if (purchaseDeliveryRecordProducts.length > 0) {
              layer.load(2);
              d.field.purchaseDeliveryRecordProducts = purchaseDeliveryRecordProducts;
              admin.req(
                data ? "enterprise/purchase/admin/purchaseDeliveryRecord/update" : "enterprise/purchase/admin/purchaseDeliveryRecord/add",
                JSON.stringify(d.field),
                function (res) {
                  layer.closeAll("loading");
                  if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1 });
                    table.reload("purchaseDeliveryRecordTable");
                    layer.closeAll("page");
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                data ? "PUT" : "POST",
                false,
                "application/json"
              );
            } else {
              layer.msg("至少添加一条发货物料！", { icon: 2 });
            }
            return false;
          });
        }
      });
    }

    // 显示详情弹窗
    function showViewModel(data) {
      admin.open({
        type: 1,
        area: ["80%", "85%"],
        offset: ["10%", "17%"],
        title: "供应商发货记录详情",
        content: $("#purchaseDeliveryRecordView").html(),
        success: function () {
          //数据回显
          if (data.transportMode == 0) {
            $("#plateNoDetailDiv").show();
            $("#courierNoDetailDiv").hide();
          } else {
            $("#plateNoDetailDiv").hide();
            $("#courierNoDetailDiv").show();
          }
          form.val("purchaseDeliveryRecordView", data);
          // 渲染表格
          table.render({
            elem: "#purchaseDeliveryRecordProductTable",
            url: config.base + "enterprise/purchase/admin/purchaseDeliveryRecordProduct/list",
            where: { purchaseDeliveryRecordId: data.id },
            page: false,
            cellMinWidth: 100,
            cols: [
              [
                { type: "numbers", title: "序号", width: 55 },
                { field: "id", title: "采购订单明细ID", hide: "true" },
                {
                  field: "productPic",
                  title: "物料图片",
                  align: "center",
                  templet: function (d) {
                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>';
                  }
                },
                {
                  field: "skuCode",
                  title: "物料编号",
                  align: "center",
                  width: 160
                },
                {
                  field: "specifications",
                  title: "规格号",
                  align: "center",
                  width: 160
                },
                {
                  field: "productTypeName",
                  title: "物料类型",
                  align: "center",
                  width: 160
                },
                { field: "mainUnitName", title: "计价单位", align: "center" },
                { field: "mainUnitRate", title: "主换算率", align: "center" },
                {
                  field: "purchaseQuantity",
                  title: "采购数量",
                  align: "center"
                },
                { field: "quantity", title: "发货数量", align: "center" }
              ]
            ]
          });
        }
      });
    }

    // 删除
    function doDelete(obj) {
      layer.confirm(
        "确定要删除吗？",
        {
          offset: "65px",
          skin: "layui-layer-admin"
        },
        function (i) {
          layer.close(i);
          layer.load(2);
          admin.req(
            "enterprise/purchase/admin/purchaseDeliveryRecord/" + obj.data.id,
            {},
            function (res) {
              layer.closeAll("loading");
              if (res.code == 200) {
                layer.msg(res.msg, { icon: 1 });
                obj.del();
              } else {
                layer.msg(res.msg, { icon: 2 });
              }
            },
            "DELETE"
          );
        }
      );
    }

    // 导出excel
    $("#purchaseDeliveryRecordBtnExport").click(function () {
      var checkRows = table.checkStatus("purchaseDeliveryRecordTable");
      if (checkRows.data.length == 0) {
        layer.msg("请选择要导出的数据", { icon: 2 });
      } else {
        table.exportFile(insTb.config.id, checkRows.data, "xls");
      }
    });
  });
</script>
